package com.neo.mapper;

import com.neo.domain.Employee;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

/**
 * @Author: hredis
 * @Date: 2020/10/24 20:23
 */
public class EmployeeMapperProvider {
    public String updateEmployeeById(Employee employee) {
        return new SQL(){{
            UPDATE("tbl_employee");
            if(employee.getLastName()!=null){
                SET("last_name = #{lastName}");
            }
            if(employee.getEmail()!=null){
                SET("email = #{email}");
            }
            if(employee.getGender()!=null){
                SET("gender = #{gender}");
            }
            if(employee.getAge()!=null){
                SET("age = #{age}");
            }
            if(employee.getId()!=null){
                WHERE("id = #{id}");
            }
            //从这个toString可以看出，其内部使用高效的StringBuilder实现SQL拼接
        }}.toString();
    }

    public String findByCondition(String queryString){
        return new SQL(){{
            SELECT("*");
            FROM("tbl_employee");
            if(!StringUtils.isEmpty(queryString)){
                WHERE("last_name like concat('%',#{queryString},'%') or email like concat('%',#{queryString},'%')");
            }
        }}.toString();
    }
}